﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.ServiceModel.Web;
using System.Threading;
using System.Data.SQLite;
using System.Configuration;
using System.Web;
using System.Collections.Specialized;
using System.Data.SqlClient;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.ServiceModel;
using System.Xml;
using System.ServiceModel.Channels;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Net;

namespace WMIService
{
    public class Service : IService
    {
        public Stream GetWMIInfo()
        {
            WMI.Classes.GetAllClasses();
            DataSet ds = WMI.Classes.LocalWMI;
            WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
            try
            {

                return new MemoryStream(Encoding.UTF8.GetBytes(Regex.Replace(ds.GetXml(), "[\x01-\x1F]", "")));
            }
            catch (Exception ex)
            {
                return new MemoryStream(Encoding.UTF8.GetBytes(ex.Message));
            }
        }

        public Stream GetClassesByQuery()
        {
            NameValueCollection p = HttpUtility.ParseQueryString(WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters.ToString());
            WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
            try
            {
                return new MemoryStream(Encoding.UTF8.GetBytes(Regex.Replace(WMI.Classes.GetByQuery(p[0]),"[\x01-\x1F]", "")));
            }
            catch (Exception ex)
            {
                return new MemoryStream(Encoding.UTF8.GetBytes(ex.Message));
            }
        }

        public Stream GetScheduledClasses()
        {
            WMI.Classes.LocalWMI.Tables.Clear();
            WMI.Classes.GetClassesByScheduling();
            DataSet ds = WMI.Classes.LocalWMI;
            WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
            try
            {
                return new MemoryStream(Encoding.UTF8.GetBytes(Regex.Replace(ds.GetXml(), "[\x01-\x1F]", "")));
            }
            catch (Exception ex)
            {
                return new MemoryStream(Encoding.UTF8.GetBytes(ex.Message));
            }
        }

        public Stream ConfigureScheduler()
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
            return new MemoryStream(Encoding.UTF8.GetBytes(Utilities.Properties.Resources.EditXML));
        }

        public Stream SaveXML(Message message)
        {
            Message sa = OperationContext.Current.RequestContext.RequestMessage;
            string returnVal = string.Empty;
            using (var reader = sa.GetReaderAtBodyContents())
            {
                if (reader.Read())
                {
                    var data = Encoding.UTF8.GetChars(reader.ReadContentAsBase64());
                    StringBuilder sb = new StringBuilder().Append(data);
                    NameValueCollection coll = HttpUtility.ParseQueryString(sb.ToString());
                    try
                    {
                        File.WriteAllText(coll[0], coll[1]);
                        WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
                        returnVal = "<script type=\"text/javascript\">alert(\"File salvato con successo\"); history.back(); </script>";
                    }
                    catch (Exception ex) 
                    { 
                        WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
                        returnVal = ex.Message; 
                    }
                }
            }
            return new MemoryStream(Encoding.UTF8.GetBytes(returnVal));
        }

        public Stream GetXMLDocument(Message message)
        {
            string returnVal = File.ReadAllText(WebOperationContext.Current.IncomingRequest.Headers["Path"]);
            return new MemoryStream(Encoding.UTF8.GetBytes(returnVal));
        }
        public Stream Help()
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
            return new MemoryStream(Encoding.UTF8.GetBytes(Utilities.Properties.Resources.Home));
        }
    }
}
